accountingIntegrationDashboardApp.service('serverDataService', function($http, $q, $log) {
    var dataServiceFactory = {};

    var urlService = 'http://localhost:8080/wbs-service';

    /**
     * Get entity list 
     */
    dataServiceFactory.obtenerRangoFecha = function (recurso) {
        var defer = $q.defer();

        $http.get(urlService + "/" + recurso).then(function (response) {
            defer.resolve(response.data);
        });

        return defer.promise;
    };

    /**
     * Get entity list 
     */
    dataServiceFactory.getRecordsByCode = function (entidad, codigo) {
        var defer = $q.defer();

        $http.get(urlService + "/" + entidad + "/" + codigo).then(function (response) {
            defer.resolve(response.data);
        });

        return defer.promise;
    };

    /**
     * Get entity list 
     */
    dataServiceFactory.getAllRecords = function (entity) {
        var defer = $q.defer();

        $http.get(urlService + "/" + entity + "/all").then(function (response) {
            defer.resolve(response.data);
        });

        return defer.promise;
    };

    /**
     * Get entity list 
     */
    dataServiceFactory.getEntityList = function (entity, eager) {
        var defer = $q.defer();

        $http.get(urlService + "/" + entity, {params: {'eager' : eager}}).then(function (response) {
            defer.resolve(response.data);
        });

        return defer.promise;
    };

    /**
     * Get entity list by dates
     */
    dataServiceFactory.getRecordsByFechas = function (scope, entity, fechaInicio, fechaFin) {
        var defer = $q.defer();

        $http.get(urlService + "/" + scope.module + "/" + entity+"/"+fechaInicio+"/"+fechaFin).then(function (response) {
            defer.resolve(response.data);
        });

        return defer.promise;
    };
    
    /**
     * Get entity list by page 
     */
    dataServiceFactory.getEntityListByPage = function (entity, eager, page, size) {
        var defer = $q.defer();

        $http.get(urlService + "/" + entity, {params: {'eager' : eager, 'page' : page, 'size' : size}}).then(function (response) {
            defer.resolve(response.data);
        });

        return defer.promise;
    };

    dataServiceFactory.saveOrupdateAndRenderEntity = function(scope, entity, id) {
        var defer = $q.defer();
        angular.extend(entity, {id: id});

        $log.info('Entity: ' + scope.entityName, ' id: ' + entity.id);

        if ( entity.id == null) {
            $http.post(urlService + "/" + scope.module + "/" + scope.entityName, entity)
            .then(function (response){
                $log.debug('Post Response: OK');
                scope.successResponse=response.data.stringResult;
                $log.info(response.data);
                defer.resolve(true);
            }, function (response){    
                $log.debug('Post Error Response status:' + response.status);
                $log.debug(response.data);
                scope.errorResponse=response.data;
                defer.reject(response.data);
            });
        } else {
            $http.put(urlService + "/" + scope.moduleName + "/" + scope.entityName + "/" + entity.id, entity)
            .then(function (response){
                $log.debug('Put Response: OK');
                scope.successResponse=response.data.stringResult;
                $log.info(response.data);
                defer.resolve(true);
            }, function (response){
                $log.debug('Put Error Response status:' + response.status);
                $log.debug(response.data);
                scope.errorResponse=response.data;
                defer.reject(response.data);
            });
        }

        return defer.promise;
    };

    dataServiceFactory.grabarTransaccion = function(scope, transaccion) {
        var defer = $q.defer();

        $http.post(urlService + "/" + scope.module + "/" + scope.entityName, transaccion)
        .then(function (response){
            scope.successResponse = response.data.stringResult;
            defer.resolve(true);
        }, function (response){
            scope.errorResponse = response.data;
            defer.reject(response.data);
        });

        return defer.promise;
    };

    dataServiceFactory.actualizarRegistro = function(scope, registro) {
        var defer = $q.defer();

        $http.put(urlService + "/" + scope.module + "/" + scope.entityName + "/" + registro.codigo, registro)
        .then(function (response){
            scope.successResponse = response.data.stringResult;
            defer.resolve(true);
        }, function (response){
            scope.errorResponse = response.data;
            defer.reject(response.data);
        });

        return defer.promise;
    };

    dataServiceFactory.eliminarRegistro = function(scope, entity, codigo) {
        var defer = $q.defer();

        $http({method: 'DELETE', url: urlService + "/" + scope.module  +"/" + entity + "/" + codigo})
        .then(function (response){
            scope.successResponse=response.data.stringResult;
            defer.resolve(true);
        }, function (response){
            defer.reject(response.data);
        });

        return defer.promise;
    };

    dataServiceFactory.getEntityListElement= function (entity, glElementId) {
        var defer = $q.defer();

        $http.get(urlService + entity+ "/" + glElementId).then(function (response) {
            defer.resolve(response.data);
        });

        return defer.promise;
    };

    return dataServiceFactory;
});